package com.letian.info.service.impl;

import java.util.List;

import com.letian.common.constant.Constants;
import com.letian.common.constant.DelFlagConstants;
import com.letian.common.constant.IsConstants;
import com.letian.common.utils.DateUtils;
import com.letian.info.domain.*;
import com.letian.info.mapper.InfoUserCollectMapper;
import com.letian.info.mapper.InfoUserFollowMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.letian.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.letian.info.mapper.InfoArticleMapper;
import com.letian.info.service.IInfoArticleService;

/**
 * 文章信息Service业务层处理
 *
 * @author bmz
 * @date 2023-04-05
 */
@Service
public class InfoArticleServiceImpl implements IInfoArticleService {
    @Autowired
    private InfoArticleMapper infoArticleMapper;

    @Autowired
    private InfoUserFollowMapper infoUserFollowMapper;

    @Autowired
    private InfoUserCollectMapper infoUserCollectMapper;

    /**
     * 查询文章信息
     *
     * @param articleId 文章信息主键
     * @return 文章信息
     */
    @Transactional
    @Override
    public InfoArticle selectInfoArticleByArticleId(Long articleId) {
        InfoArticle infoArticle = infoArticleMapper.selectInfoArticleByArticleId(articleId);
        infoArticle.setBrowseCount(infoArticle.getBrowseCount() + 1);
        infoArticleMapper.updateInfoArticle(infoArticle);

        return infoArticle;
    }

    /**
     * 点赞或者收藏
     * @param userId
     * @param userName
     * @param articleId
     * @param isThumbsUp
     * @return
     */
    @Transactional
    @Override
    public int thumbsUp(String userName, Long articleId, Integer isThumbsUp) {
        InfoUserCollect query = new InfoUserCollect();
        query.setUserName(userName);
        query.setObjectType(Constants.OBJECT_TYPE_ARTICLE_STR);
        query.setObjectId(articleId);
        query.setDelFlag(DelFlagConstants.EXIST);
        List<InfoUserCollect> list = infoUserCollectMapper.selectInfoUserCollectList(query);

        InfoArticle infoArticle = infoArticleMapper.selectInfoArticleByArticleId(articleId);
        infoArticle.setThumbsUpCount(infoArticle.getThumbsUpCount() + 1);
        infoArticleMapper.updateInfoArticle(infoArticle);
        int result = 0;
        if (CollectionUtils.isEmpty(list)) {
            query.setIsThumbsUp(isThumbsUp);
            query.setCreateBy(userName);
            query.setCreateTime(DateUtils.getNowDate());
            result = infoUserCollectMapper.insertInfoUserCollect(query);
        } else {
            query = list.get(0);
            query.setIsThumbsUp(isThumbsUp);
            result = infoUserCollectMapper.updateInfoUserCollect(query);
        }
        return result;
    }

    /**
     * 收藏文章
     * @param userName
     * @param articleId
     * @return
     */
    @Transactional
    @Override
    public int likeArticle( String userName, Long articleId) {
        InfoUserCollect query = new InfoUserCollect();
        query.setUserName(userName);
        query.setObjectType(Constants.OBJECT_TYPE_ARTICLE_STR);
        query.setObjectId(articleId);
        query.setDelFlag(DelFlagConstants.EXIST);
        List<InfoUserCollect> list = infoUserCollectMapper.selectInfoUserCollectList(query);

        int result = 0;
        if (CollectionUtils.isEmpty(list)) {
            query.setIsLike(IsConstants.IS);
            query.setCreateBy(userName);
            query.setCreateTime(DateUtils.getNowDate());
            result = infoUserCollectMapper.insertInfoUserCollect(query);
        } else {
            query = list.get(0);
            query.setIsLike(query.getIsLike() == IsConstants.IS ? IsConstants.ISNOT:IsConstants.IS);
            result = infoUserCollectMapper.updateInfoUserCollect(query);
        }
        return result;
    }

    /**
     * 查询文章信息
     *
     * @param userName    文章信息主键
     * @param articleId 文章信息主键
     * @return 文章信息
     */
    @Override
    public InfoArticle queryUserArticleDetail(String userName, Long articleId) {
        InfoArticle infoArticle = infoArticleMapper.selectInfoArticleByArticleId(articleId);
        infoArticle.setBrowseCount(infoArticle.getBrowseCount() + 1);
        infoArticleMapper.updateInfoArticle(infoArticle);


        InfoUserFollow query = new InfoUserFollow();
        query.setUserName(userName);
        query.setFollowUserName(infoArticle.getAuthorName());
        List<InfoUserFollow> infoUserLikes = infoUserFollowMapper.selectInfoUserFollowList(query);
        if (!CollectionUtils.isEmpty(infoUserLikes)) {
            infoArticle.setIsAuthorLike(Constants.SYS_YES);
        } else {
            infoArticle.setIsAuthorLike(Constants.SYS_NO);
        }

        InfoUserCollect infoUserArticleQuery = new InfoUserCollect();
        infoUserArticleQuery.setUserName(userName);
        infoUserArticleQuery.setObjectType(Constants.OBJECT_TYPE_ARTICLE_STR);
        infoUserArticleQuery.setObjectId(articleId);
        List<InfoUserCollect> infoUserArticles = infoUserCollectMapper.selectInfoUserCollectList(infoUserArticleQuery);
        if (!CollectionUtils.isEmpty(infoUserArticles)) {
            infoArticle.setIsLike(infoUserArticles.get(0).getIsLike());
            infoArticle.setIsThumbsUp(infoUserArticles.get(0).getIsThumbsUp());
        } else {
            infoArticle.setIsLike(IsConstants.ISNOT);
            infoArticle.setIsThumbsUp(IsConstants.ISNOT);
        }

        return infoArticle;
    }

    /**
     * 查询文章信息
     *
     * @param title 文章信息主键
     * @return 文章信息
     */
    public List<InfoArticle> queryArticleList(String title, String userName) {
        return infoArticleMapper.queryArticleList(title, userName);
    }

    /**
     * 查询文章信息列表
     *
     * @param infoArticle 文章信息
     * @return 文章信息
     */
    @Override
    public List<InfoArticle> selectInfoArticleList(InfoArticle infoArticle) {
        return infoArticleMapper.selectInfoArticleList(infoArticle);
    }

    /**
     * 通过标签查询文章信息列表
     *
     * @param infoArticle 文章信息
     * @return 文章信息集合
     */
    public List<InfoArticle> selectInfoArticleListByLabel(InfoArticle infoArticle, String userName){
        return infoArticleMapper.selectInfoArticleListByLabel(infoArticle,userName);
    }

    /**
     * 查询关注文章信息列表
     *
     * @param infoArticle 文章信息
     * @param userName
     * @return 文章信息集合
     */
    public List<InfoArticle> likeList(InfoArticle infoArticle, String userName){
        return infoArticleMapper.selectInfoLikeArticleList(infoArticle, userName);
    }

    /**
     * 新增文章信息
     *
     * @param infoArticle 文章信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertInfoArticle(InfoArticle infoArticle) {
        infoArticle.setCreateTime(DateUtils.getNowDate());
        int rows = infoArticleMapper.insertInfoArticle(infoArticle);
        insertInfoComment(infoArticle);
        return rows;
    }

    /**
     * 修改文章信息
     *
     * @param infoArticle 文章信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateInfoArticle(InfoArticle infoArticle) {
        infoArticle.setUpdateTime(DateUtils.getNowDate());
        infoArticleMapper.deleteInfoCommentByObjectId(Constants.OBJECT_TYPE_ARTICLE_STR, infoArticle.getArticleId());
        insertInfoComment(infoArticle);
        return infoArticleMapper.updateInfoArticle(infoArticle);
    }

    /**
     * 批量删除文章信息
     *
     * @param articleIds 需要删除的文章信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInfoArticleByArticleIds(Long[] articleIds) {
        infoArticleMapper.deleteInfoCommentByObjectIds(Constants.OBJECT_TYPE_ARTICLE_STR, articleIds);
        return infoArticleMapper.deleteInfoArticleByArticleIds(articleIds);
    }

    /**
     * 删除文章信息信息
     *
     * @param articleId 文章信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInfoArticleByArticleId(Long articleId) {
        infoArticleMapper.deleteInfoCommentByObjectId(Constants.OBJECT_TYPE_ARTICLE_STR,articleId);
        return infoArticleMapper.deleteInfoArticleByArticleId(articleId);
    }

    /**
     * 新增评论信息信息
     *
     * @param infoArticle 文章信息对象
     */
    public void insertInfoComment(InfoArticle infoArticle) {
        List<InfoComment> infoCommentList = infoArticle.getInfoCommentList();
        Long articleId = infoArticle.getArticleId();
        if (StringUtils.isNotNull(infoCommentList)) {
            List<InfoComment> list = new ArrayList<InfoComment>();
            for (InfoComment infoComment : infoCommentList) {
                infoComment.setObjectId(articleId);
                list.add(infoComment);
            }
            if (list.size() > 0) {
                infoArticleMapper.batchInfoComment(list);
            }
        }
    }
}
